package com.itboye.sunsunhome.www.aq;

import java.io.File;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.itboye.base.BaseActivity;
import com.itboye.sunsunhome.www.aq.base.MyApplication;
import com.itboye.sunsunhome.wxapi.Util;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXImageObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class ShapeActivity extends BaseActivity {
	private IWXAPI api;
	private String url = "http://www.sunsun-china.com/";
	int WX_THUMB_SIZE = 150;
	LinearLayout relyout_Shape, lineTimeLin;
	private ImageView imgBitmap,shapeBack;
	String mphoto;
	Bitmap bm = null;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		// com.itboye.sunsunhome.www.aq.FeedbackActivity
		super.onCreate(arg0);
		// setContentView(R.layout.activity_share);
		setContentView(R.layout.activity_share);
		imgBitmap = (ImageView) findViewById(R.id.imgBitmap);
		relyout_Shape = (LinearLayout) findViewById(R.id.relyout_Shape);
		shapeBack=(ImageView) findViewById(R.id.shapeBack);
		shapeBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		lineTimeLin = (LinearLayout) findViewById(R.id.lineTimeLin);
		mphoto = getIntent().getStringExtra("mphoto");
		api = WXAPIFactory.createWXAPI(this, MyApplication.WX_APP_ID);
		File file = new File(mphoto);
		if (file.exists()) {
			bm = BitmapFactory.decodeFile(mphoto);
			System.out.println(">>>>>>>>>>>>>>>>>>" + bm);
			imgBitmap.setImageBitmap(bm);
		}
		/**
		 * 分享到朋友圈
		 */
		lineTimeLin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (bm != null) {
					sendReq(MyApplication.context, "森森水族分享", bm,
							SendMessageToWX.Req.WXSceneTimeline);
				}else{
					Toast.makeText(MyApplication.context,"图片不能为空", 0).show();
				}
			}
		});
		/**
		 * 分享给好友
		 */
		relyout_Shape.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (bm != null) {
					sendReq(MyApplication.context, getString(R.string.app_name)+getString(R.string.share), bm,
							SendMessageToWX.Req.WXSceneSession);
				}else{
					Toast.makeText(MyApplication.context,"图片不能为空", 0).show();
				}
			}
		});

	}

	/**
	 * 微信分享
	 * 
	 * @param context
	 * @param text
	 * @param bmp
	 * @param type
	 */
	public void sendReq(Context context, String text, Bitmap bmp, int type) {
		if (api.openWXApp()) {
			isWXAppInstalledAndSupported();
			WXWebpageObject localWXWebpageObject = new WXWebpageObject();
			localWXWebpageObject.webpageUrl = url;
			WXMediaMessage localWXMediaMessage = new WXMediaMessage(
					localWXWebpageObject);

			localWXMediaMessage.description = text;
			// 设置消息的缩略图
			 bmp = Bitmap.createScaledBitmap(bmp, WX_THUMB_SIZE,
					WX_THUMB_SIZE, true);
			
			localWXMediaMessage.thumbData = Util.bmpToByteArray(bmp, true);
			SendMessageToWX.Req localReq = new SendMessageToWX.Req();
			localReq.transaction = System.currentTimeMillis() + "";
			localReq.message = localWXMediaMessage;
			localReq.scene = type;
			bmp.recycle();
			api.sendReq(localReq);
		} else {
			Toast.makeText(MyApplication.context, "未安装微信", Toast.LENGTH_SHORT)
					.show();
		}
	}

	public void onkk(Context context, String text, Bitmap bmp, int type) {
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.push);
		WXImageObject imgObj = new WXImageObject(bmp);

		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = imgObj;

		Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, WX_THUMB_SIZE,
				WX_THUMB_SIZE, true);
		bmp.recycle();
		msg.thumbData = Util.bmpToByteArray(thumbBmp, true); // 设置缩略图

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("img");
		req.message = msg;
		api.sendReq(req);
	}

	/**
	 * 判断是否安转微信
	 * 
	 * @return
	 */

	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis())
				: type + System.currentTimeMillis();
	}

	@SuppressWarnings("unused")
	private boolean isWXAppInstalledAndSupported() {
		IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
		msgApi.registerApp(MyApplication.WX_APP_ID);

		boolean sIsWXAppInstalledAndSupported = msgApi.isWXAppInstalled()
				&& msgApi.isWXAppSupportAPI();

		return sIsWXAppInstalledAndSupported;
	}

}
